<?php

namespace app\common\model\plus\activity;

use app\api\model\order\Order as OrderModel;
use app\common\model\BaseModel;

/**
 * 参与记录模型
 */
class Active extends BaseModel
{
    protected $name = 'activity';
    protected $pk = 'activity_id';
    //附加字段
    protected $append = ['status_text', 'start_time_text', 'end_time_text', 'join_status', 'join_end_time_text', 'start_status', 'start_status_text'];

    /**
     * 有效期-开始时间
     * @param $value
     * @return array
     */
    public function getStartTimeTextAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['start_time']);
    }

    /**
     * 有效期-开始时间
     * @param $value
     * @return array
     */
    public function getEndTimeTextAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['end_time']);
    }

    /**
     * 有效期-开始时间
     * @param $value
     * @return array
     */
    public function getJoinEndTimeTextAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['join_end_time']);
    }

    /**
     * 状态
     * @param $val
     * @return string
     */
    public function getStatusTextAttr($value, $data)
    {
        if ($data['status'] == 0) {
            return '未生效';
        }
        if ($data['start_time'] > time()) {
            return '未开始';
        }
        if ($data['end_time'] < time()) {
            return '已结束';
        }
        if ($data['start_time'] < time() && $data['end_time'] > time()) {
            return '生效-进行中';
        }
        return '';
    }

    /**
     * 开始状态
     * @param $val
     * @return string
     */
    public function getStartStatusAttr($value, $data)
    {
        if ($data['status'] == 0) {
            return -1;//未生效
        }
        if ($data['start_time'] > time()) {
            return 0;//未开始
        }
        if ($data['end_time'] < time()) {
            return 2;//已结束
        }
        if ($data['start_time'] < time() && $data['end_time'] > time()) {
            return 1;//生效-进行中
        }
        return -1;
    }

    /**
     * 开始状态文字
     * @param $val
     * @return string
     */
    public function getStartStatusTextAttr($value, $data)
    {
        if ($data['status'] == 0) {
            return "未生效";//未生效
        }
        if ($data['start_time'] > time()) {
            return "即将开始";
        }
        if ($data['end_time'] < time()) {
            return "已结束";
        }
        if ($data['start_time'] < time() && $data['end_time'] > time()) {
            return "抢购中";
        }
        return "未生效";
    }

    /**
     * 状态
     * @param $val
     * @return string
     */
    public function getJoinStatusAttr($value, $data)
    {
        if ($data['status'] == 0) {
            return 0;
        }
        if ($data['end_time'] < time()) {
            return 0;
        }
        if ($data['join_end_time'] < time()) {
            return 0;
        }

        return 1;
    }

    /**
     * 处理过的详情数据
     */
    public static function detailWithTrans($activity_id,$user=[])
    {
        $model = (new static())->with(['file', 'imgUrlPath', 'shareImgPath'])->where('activity_id', '=', $activity_id)->find();
//        dump($model['imgUrlPath']);exit;
        $OrderModel = new OrderModel;
        $firstOrder = 0;
        if($user){
            $firstOrder = $OrderModel->getCount($user, 'all',true);
        }

        $detail = [
            'title' => $model['title'],
            'image_id' => $model['image_id'],
            'identifying' => $model['identifying'],
            'label' => $model['label'],
            'status' => $model['status'],
            'sort' => $model['sort'],
            'start_time' => $model['start_time'],
            'end_time' => $model['end_time'],
            'file_path' => $model['file']['file_path'],
            'join_end_time' => date('Y-m-d H:i:s', $model['join_end_time']),
            'is_coupon' => $model['is_coupon'],
            'imgUrl' => $model['imgUrl'],
            'bgColor' => $model['bgColor'],

            'imgUrl_path' => $model['imgUrlPath']['file_path'] ?? "",
            'share_img_url' => $model['shareImgPath']['file_path'] ?? "",

            'share_title' => $model['share_title'],
            'share_img' => $model['share_img']??'',
            'activity_type' => $model['activity_type']??'',
            'navbar_title' => $model['navbar_title']??'',
            'navbar_bgcolor' => $model['navbar_bgcolor']??'',
            'left_color' => $model['left_color']??'',
            'right_color' => $model['right_color']??'',
            'is_new' => $model['is_new']??0,
            'stock' => $model['stock']??0,
            'balance_stock' => $model['balance_stock']??0,
            'is_buy' => $firstOrder,

            'active_date' => [
                date('Y-m-d H:i:s', $model['start_time']),
                date('Y-m-d H:i:s', $model['end_time']),
            ],
            'active_time' => [
                $model['day_start_time'],
                $model['day_end_time'],
            ]
        ];

        return $detail;
    }

    public static function detail($activity_id)
    {
        return (new static())->find($activity_id);
    }

    public function file()
    {
        return $this->belongsTo('app\\common\\model\\file\\UploadFile', 'image_id', 'file_id');
    }


    public function imgUrlPath()
    {
        return $this->belongsTo('app\\common\\model\\file\\UploadFile', 'imgUrl', 'file_id');
    }


    public function shareImgPath()
    {
        return $this->belongsTo('app\\common\\model\\file\\UploadFile', 'share_img', 'file_id');
    }

    public function activityProduct()
    {
        return $this->hasMany('app\\common\\model\\plus\\activity\\Product', 'activity_id', 'activity_id');
    }


}